﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/30
 * 
 * 功能说明：表单管理页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_CommonModel_ModelFormManage : System.Web.UI.Page
{
    protected int ModelID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        ModelID = NetRequest.GetQueryInt("ModelID");

        if (!IsPostBack)
            BindData();
    }

    /// <summary>
    /// 绑定表单列表
    /// </summary>
    private void BindData()
    {
        string sql = "SELECT * FROM Whir_ModelForm WHERE ModelID =" + ModelID;
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(sql).Tables[0];
        if (dt.Rows.Count > 0)
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
        else
        {
            CreateModelForm(ModelID);
        }
    }

    /// <summary>
    /// 设置默认表单
    /// </summary>
    /// <param name="modelID">功能模块编号</param>
    private void CreateModelForm(int modelID)
    {
        Whir.BLL.Whir_Model modelbll = new Whir.BLL.Whir_Model();
        Whir.Model.Whir_Model modelModel = modelbll.GetModel(modelID);

        Whir.BLL.Whir_ModelForm bll = new Whir.BLL.Whir_ModelForm();
        Whir.Model.Whir_ModelForm model = new Whir.Model.Whir_ModelForm();

        Whir.BLL.Whir_Field fieldBll = new Whir.BLL.Whir_Field();
        DataTable dt = fieldBll.GetList(" modelID =" + modelID).Tables[0];

        if (dt.Rows.Count > 0)
        {
            string temp = "";
            foreach (DataRow row in dt.Rows)
            {
                temp += row["FieldID"].ToString() + "|" + "true" + ",";
            }
            temp = temp.TrimEnd(',');

            model.modelID = modelID;
            model.formName = modelModel.modelName;
            model.formDesc = modelModel.modelDesc;
            model.fieldIDs = temp;
            model.formContent = "";
            model.formsearch = "";
            model.createUserID = 0;
            model.updateUserID = 0;
            bll.Add(model);
            JsHelper.redirect(Page, "生成默认表单成功！", "ModelFormManage.aspx?ModelID=" + ModelID);
        }
        else
        {
            JsHelper.redirect(Page, "该功能模块下没有字段！", "FieldManage.aspx?ModelID=" + ModelID);
        }
    }


    /// <summary>
    /// 根据字段ID获取字段名称
    /// </summary>
    /// <param name="fieldIDs">字段ID列表</param>
    /// <returns>返回字段名称</returns>
    protected string GetFieldsName(object obj)
    {
        Whir.BLL.Whir_ModelForm modelFormBll = new Whir.BLL.Whir_ModelForm();
        int modelFormID = int.Parse(obj.ToString());
        string[] strs = modelFormBll.GetFieldIDs(modelFormID).Split(',');
        string[] fields = new string[strs.Length];
        for (int i = 0; i < strs.Length; i++)
        {
            fields[i] = strs[i].Split('|')[0];
        }

        if (fields.Length <= 1)
            return "";

        string temp = "";

        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        Whir.Model.Whir_Field model = new Whir.Model.Whir_Field();

        for (int i = 0; i < fields.Length; i++)
        {
            model = bll.GetModel(int.Parse(fields[i]));
            temp += model.alias + ",";
        }
        temp = temp.TrimEnd(',');
        return temp;
    }

    /// <summary>
    /// 添加表单
    /// </summary>
    protected void btnAddModelForm_Click(object sender, EventArgs e)
    {
        Response.Redirect("ModelForm.aspx?Action=Add&ModelID=" + ModelID);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Contents/ModelManage.aspx");
    }
}